package rattlesnake.callback.server.filter;

import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rattlesnake.callback.core.NettyBootAppcation;
import rattlesnake.callback.core.cache.RedisCache;
import rattlesnake.callback.util.CodecUtil;
import rattlesnake.callback.util.StringUtil;
import java.util.Iterator;
import java.util.Map;

/**
 * @Package: rattlesnake.callback.server.filter
 * @Description: 信息过滤器，对传递的信息进行包装
 * @author: liuxin
 * @date: 17/4/11 下午7:02
 */
public class MessageFilter {
    private static final RedisCache redisCache = NettyBootAppcation.getRedisCache();
    private static final Logger logger = LoggerFactory.getLogger(MessageFilter.class);

    /**
     * 根据请求方法过滤重复
     * GET使用url字符串加密
     * POST使用 parameter
     *
     * @param object
     * @param request
     * @return true标识通过，false表示重复推送
     */
    public static boolean repeatFilter(Object object, HttpRequest request) {
        boolean flag = false;
        String message = "";
        if (request.getMethod().equals(HttpMethod.GET)) {
            message = messageByGet(object);
            flag=findCache(message);
        } else if (request.getMethod().equals(HttpMethod.POST)) {
            message = messageByPost(object);
            flag=findCache(message);
        } else {
            flag = true;//其他方式一律通过
        }
        return flag;
    }

    /**
     * 存才就返回 false
     * 不存在就返回true,并且添加
     *
     * @param messageCode
     * @return
     */
    private static boolean findCache(String messageCode) {
        String value= redisCache.get(messageCode);
        if (StringUtil.isEmpty(value)){
            redisCache.set(messageCode);
            logger.debug("第一次推送,加入缓存\tkey:{}=>{}",messageCode,RedisCache.DEFAULT_VALUE);
            return true;
        }
        logger.debug("缓存存在,重复推送\tkey:{}=>{}",messageCode,value);
        return false;
    }

    /**
     * 从url中获取消息摘要
     *
     * @param object
     * @return
     */
    private static String messageByGet(Object object) {
        String url = (String) object;
        String messageCode = CodecUtil.encryptMD5(url);
        logger.debug("GET参数串:{}", url);
        logger.debug("GET生成MessageCode:{}", messageCode);
        return messageCode;
    }

    /**
     * 从post的parameter中获取参数
     *
     * @param object
     * @return
     */
    private static String messageByPost(Object object) {
        StringBuilder sb = new StringBuilder();
        String resultStr = "";
        String messageCode = "";
        Map<String, String> parameter = (Map<String, String>) object;
        Iterator<String> it = parameter.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = parameter.get(key);
            sb.append(key).append("=").append(value).append("&");
        }
        resultStr = sb.toString().substring(0, sb.length() - 1);
        messageCode = CodecUtil.encryptMD5(resultStr);
        logger.debug("POST参数串:{}", resultStr);
        logger.debug("POST生成MessageCode:{}", messageCode);
        return messageCode;
    }
}
